package com.wsd.jing.auth;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * All rights Reserved, Designed By www.tom.com
 *
 * @author tom
 * @version 1.0.0
 * @projectName: jing
 * @packageName: com.wsd.jing.config
 * @className: SaFilterConfig
 * @description SaToken资源过滤配置
 * @createDate 2022/11/18 20:12
 * @copyright 2022/11/18 20:12 www.tom.com Inc. All rights reserved.
 */
@Configuration
public class SaFilterConfig implements WebMvcConfigurer {

    /**
     * @param registry:
     * @return void
     * @author tom
     * @description 开启SaToken注解鉴权
     * @createDate 2022/11/18 20:50
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SaInterceptor(handler -> {
            SaRouter
                    .match("/**")
                    .notMatch("/doc.html")
                    .notMatch("/webjars/**")
                    .notMatch("/swagger-resources")
                    .notMatch("/v2/api-docs")
                    .notMatch("/druid/**")
                    .notMatch("/rest/auth/login")
                    .notMatch("/rest/auth/captcha")
                    .check(r -> StpUtil.checkLogin());
        }).isAnnotation(true)
        ).addPathPatterns("/**");
    }
}
